<template>
    <el-form v-if="props.clientId == null" inline  class="demo-form-inline">
        <el-form-item label="坐席姓名">
            <el-input v-model="query.agentName" placeholder="坐席姓名" />
        </el-form-item>
        <el-form-item label="时间范围">
            <el-date-picker
                    v-model="query.dateRange"
                    type="datetimerange"
                    range-separator="到"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
            />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" plain  activity @click="queryChat">查询</el-button>
            <el-button type="primary" plain  activity @click="reset">重置</el-button>
        </el-form-item>
    </el-form>
    <el-button type="primary" size="small" @click="download">
        导出<el-icon class="el-icon--right"><Download /></el-icon>
    </el-button>
    <el-divider />
    <el-table ref="table" v-loading="loading" :data="data.tableData" header-cell-class-name="headerClass">
        <el-table-column align="center" type="index" label="序号" width="100px" v-if="props.clientId == null" />
        <el-table-column align="center"  prop="id" label="会话ID" @click="showMessage(scope.row.id)" >
            <template #default="scope">
                <el-link title="详情" type="primary" @click="showMessage(scope.row.id)">
                   {{scope.row.id}}
                </el-link>
            </template>
        </el-table-column>
        <el-table-column align="center"  prop="agentName" label="坐席姓名" />
        <el-table-column align="center"  prop="clientName" label="客户姓名" />
        <el-table-column align="center"  prop="type" label="会话类型">
            <template v-cloak #default="scope">
                {{transChatType(scope.row.type)}}
            </template>
        </el-table-column>
        <el-table-column align="center"  label="创建时间" prop="updateTime" >
            <template #default="scope">
                {{moment(scope.row.updateTime).format("yyyy-MM-DD HH:mm:ss")}}
            </template>
        </el-table-column>
    </el-table>
        <el-pagination
                style="display: flex;align-items:center;justify-content:center;"
                v-model:currentPage="query.currentPage"
                v-model:page-size="query.pageSize"
                :page-sizes="[10, 20, 30]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="query.total"
                @size-change="queryChat"
                @current-change="queryChat"
        />
    <Messages ref="messages" :chatId = thisChatId />
</template>

<script setup>
    import Messages from '@/components/business/HisChat.vue'
    import request from '@/utils/request.js'
    import { ElMessage } from 'element-plus'
    import {ref, reactive, onMounted} from 'vue'
    import moment from 'moment'
    let props = defineProps(['clientId'])
    let loading = ref(true)
    let messages = ref()
    let thisChatId = ref(0)
    let chatTypeDicItems = reactive([])
    let chatsAction = ""
    let data = reactive({
        open:false,
        tableData:[],
        type:"",
        id:"",
    });

    let query = reactive({dateRange:[],currentPage:1,pageSize:10,total:0})
    onMounted(()=>{

        if(props.clientId == null){
            chatsAction = "/api/chat/getChats"
        }else{
            chatsAction = "/api/online/getHisChats"
            query.clientId = props.clientId
        }

        request.get(`/api/useDic/getDicItems/chatType`).then(resp => {
            chatTypeDicItems.push(...resp)
        })
        queryChat()
    })

    function queryChat(){
        query.startTime = query.dateRange[0]
        query.endTime = query.dateRange[1]
        request.post(chatsAction, query).then(resp => {
            data.tableData = resp.records
            query.total = resp.total
            loading.value = false
        })
    }

    function transChatType (code){
        let value = '-'
        for (let i = 0; i < chatTypeDicItems.length; i++) {
            if(chatTypeDicItems[i].code == code){
                value = chatTypeDicItems[i].value
                break
            }
        }
        return value
    }

    function reset (){
        query.agentName = ""
        query.dateRange=[]
    }

    function showMessage (chatId){
        messages.value.showDialog = true
        thisChatId.value = chatId
    }

    function download (chatId){
        request.post('/api/chat/download', query).then(resp => {
            window.open(resp)
        })
    }

</script>
<style lang="css">
</style>